<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
	
	case 'addTopic' :
		addTopic();
		break;
		
	case 'modifyTopic' :
		modifyTopic();
		break;
		
	case 'deleteTopic' :
		deleteTopic();
		break;
	
	default :
	    // if action is not defined or unknown
		// move to main Topic page
		header('Location: index.php');
}


function addTopic()
{
	$values = $_POST['tg'];
    $madt        = $_POST['txtMadt'];
	$tendt		 = $_POST['mxtTendt'];
	$tomtat_dt		 = $_POST['mtxTomtat'];
	
	$namthuchien    = $_POST['txtNamthuchien'];
	
	$ngaybatdau	 = $_POST['txtNgaybatdau'];
	$arr = explode("-", $ngaybatdau);
	$ngaybatdau = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
	
	$ngayketthuc = $_POST['txtNgayketthuc'];
	$arr = explode("-", $ngayketthuc);
	$ngayketthuc = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
	
	$cap_id 	= $_POST['cboLevel'];
	$kinhphi    = $_POST['txtKinhphi'];
	$loaidt_id	 = 	$_POST['cboStyleTopic'];
	
	$tt_id   = $_POST['cboState'];
	$nn_id   = $_POST['cboTopicGroup'];
	$dv_id   = $_POST['cboDept'];
	$dtr_id = $_POST['cboPropertyTopic'];
	
	
	//echo $tendt;
	$pathFile = array(); $count = 0;
	for ( $i = 0; $i < 10; $i++){
		if (isset($_FILES['uploadFiles']['name'][$i]) && (!empty($_FILES['uploadFiles']['name'][$i]))){
			$pathFile[$i] = "../../data/".$_FILES['uploadFiles']['name'][$i];
			copy($_FILES['uploadFiles']['tmp_name'][$i], $pathFile[$i]);
			$count ++;
		}
	}
	$fileContent = '';
	for ( $i = 0; $i < $count; $i++){
		if ($fileContent =='') {
			$fileContent = $_FILES['uploadFiles']['name'][$i];
		}
		else {
			$fileContent = $fileContent.','.$_FILES['uploadFiles']['name'][$i];
		}
	}
	//echo $fileContent; 
	//exit();

	$sql1   = "INSERT INTO detai (madt, tendt, tomtat_dt, namthuchien, ngaybatdau, ngayketthuc, cap_id, kinhphi, loaidt_id, tt_id, nn_id, dv_id, dtr_id, noidung_dt)
	          VALUES ('$madt', '$tendt', '$tomtat_dt', '$namthuchien', '$ngaybatdau', '$ngayketthuc', $cap_id, $kinhphi, $loaidt_id, $tt_id, $nn_id, $dv_id, $dtr_id, '$fileContent')";			  		  
	//echo $sql1; exit();
	dbQuery($sql1);
	
	$values = str_replace('{dt_id}', mysql_insert_id(), $values);
	$sql3   = "INSERT INTO tacgia_detai(tg_id, dt_id, vt_id)
	          VALUES $values ";
	//echo $sql3;
	dbQuery($sql3);
		
	header("Location: index.php?nn_id=$nn_id");	
}

//	Modify a Topic

function modifyTopic()
{
	$tg_add = $_POST['tg'];
	$tg_sel = $_POST['tg_sel'];
	$strNoidung = $_POST['noidung_dt'];
	$id = (int)$_GET['id'];
	$madt        = $_POST['txtMadt'];
	$tendt		 = $_POST['mxtTendt'];
	$tomtat_dt		 = $_POST['mtxTomtat'];
	//$noidung_dt     = $_POST['mtxNoidung'];
	$namthuchien    = $_POST['txtNamthuchien'];
	
	$ngaybatdau	 = $_POST['txtNgaybatdau'];
	$arr = explode("-", $ngaybatdau);
	$ngaybatdau = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
	
	$ngayketthuc = $_POST['txtNgayketthuc'];
	$arr = explode("-", $ngayketthuc);
	$ngayketthuc = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
	
	$kinhphi    = $_POST['txtKinhphi'];
	$cap_id = $_POST['cboLevel'];	
	$tt_id   = $_POST['cboState'];
	
	$nn_id   = $_POST['cboTopicGroup'];	
	$loaidt_id   = $_POST['cboStyleTopic'];
	$dtr_id		= $_POST['cboPropertyTopic'];
	
	$dv_id   = $_POST['cboDept'];
 	
	// upload files if any
	$pathFile = array(); $count = 0;
	for ( $i = 0; $i < 10; $i++){
		if (isset($_FILES['uploadFiles']['name'][$i]) && (!empty($_FILES['uploadFiles']['name'][$i]))){
			$pathFile[$i] = "../../data/".$_FILES['uploadFiles']['name'][$i];
			copy($_FILES['uploadFiles']['tmp_name'][$i], $pathFile[$i]);
			$count ++;
		}
	}
	$fileContent = '';
	for ( $i = 0; $i < $count; $i++){
		if ($fileContent =='') {
			$fileContent = $_FILES['uploadFiles']['name'][$i];
		}
		else {
			$fileContent = $fileContent.','.$_FILES['uploadFiles']['name'][$i];
		}
	}
	// merge content names
	if (isset($strNoidung) && !empty($strNoidung))
	{
		if (!empty($fileContent)) {
			$fileContent = ',' . $fileContent;
		}
		$fileContent = $strNoidung . $fileContent;
	}
	
	$sql1   = "UPDATE detai 
               SET madt = '$madt', tendt = '$tendt', tomtat_dt = '$tomtat_dt', namthuchien = '$namthuchien', ngaybatdau = '$ngaybatdau', ngayketthuc = '$ngayketthuc', kinhphi = $kinhphi, cap_id = $cap_id, tt_id = $tt_id, nn_id = $nn_id, loaidt_id = $loaidt_id, dtr_id = $dtr_id, dv_id = $dv_id, noidung_dt = '$fileContent'
			   WHERE id = $id " ;
			 //echo $sql1; exit();		   
	$result = dbQuery($sql1);
	if (isset($tg_add) && $tg_add != '') {
		$insertValues = str_replace('{dt_id}', $id, $tg_add);
		if ($insertValues != "") {
			$sql3   = "INSERT INTO tacgia_detai(tg_id, dt_id, vt_id)
					  VALUES $insertValues ";
			//echo $sql3; exit();
			dbQuery($sql3);
		}
	}
	if (isset($tg_sel) && !empty($tg_sel)) {
		// xoa tat ca tac gia neu no khong thuoc danh sach cac tac gia da duoc chon
		$sql_delete = "DELETE FROM tacgia_detai WHERE dt_id = $id AND tg_id NOT IN " . $tg_sel;
		dbQuery($sql_delete);
	}
	else
	{
		$sql_delete = "DELETE FROM tacgia_detai WHERE dt_id = $id ";
		dbQuery($sql_delete);
	}
	header('Location: index.php');			  
}

/*
	Remove a Topic
*/
function deleteTopic()
{
	if (isset($_GET['id']) && $_GET['id'] > 0) {
	$id = (int)$_GET['id'];
	} else {
		header('Location:index.php');
	}			
	$sql1 = "UPDATE detai SET hieu_luc = 0 WHERE id = $id"; 
	//echo $sql1; exit();
	//$sql = "delete from nhomnc where mannc = '$mannc'";

	dbQuery($sql1);
	
	$sql2 = "UPDATE tacgia_detai SET hieu_luc = 0 WHERE dt_id = $id"; 
	dbQuery($sql2);
	
    header('Location: index.php'); 
}

?>